package com.future.controller;


import com.future.allUtils.constants.PermissionConstants;
import com.future.domain.DTO.PsychologicalKnowledgeDTO;
import com.future.domain.Result;
import com.future.domain.query.PsyKnowledgeQuery;
import com.future.service.IPsychologicalKnowledgeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/knowledge")
@Api(tags="心理知识管理")
public class PsychologicalKnowledgeController {
    @Autowired
    private IPsychologicalKnowledgeService psychologicalKnowledgeService;


    @PostMapping("/publish")
    @ApiOperation("发布心理知识")
    @PreAuthorize("hasAuthority('" + PermissionConstants.PSYCHOLOGIST_ARTICLE_PUBLISH + "') or hasAuthority('" + PermissionConstants.TEACHER_ARTICLE_PUBLISH + "')")
    public Result add(@RequestBody PsychologicalKnowledgeDTO knowledge){
        return psychologicalKnowledgeService.saveKnowledge(knowledge);
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取心理知识")
    public Result get(@PathVariable("id") Long id) {
        return psychologicalKnowledgeService.getKnowledge(id);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除心理知识")
    @PreAuthorize("hasAuthority('psychologist:article:publish') or hasAuthority('teacher:article:publish')")
    public Result delete(@PathVariable("id") Long id) {
        return psychologicalKnowledgeService.removeKnowledge(id);
    }
    /**
     *  分页查询心理知识
     */
    @PostMapping("/list")
    @ApiOperation("获取心理知识列表")
    public Result list(@RequestBody PsyKnowledgeQuery query) {
        return psychologicalKnowledgeService.listByPage(query);
    }
    /**
     *  随机获取一个心理知识
     *  @param type 类型
     *  @return 心理知识
     */
    @GetMapping("/random")
    @ApiOperation("常驻心里知识")
    public Result random(@RequestParam("type") String type) {
        return psychologicalKnowledgeService.PsyKnowledge(type);
   }

    /**
     *  更新心理知识
     *  @param knowledgeDTO 心理知识
     *  @param id 心理知识id
     *  @return 心理知识
     */
    @PutMapping("/update/{id}")
    @ApiOperation("更新心理知识")
    public Result update(@RequestBody PsychologicalKnowledgeDTO knowledgeDTO,@PathVariable("id") Long id) {
        return psychologicalKnowledgeService.updateKnowledge(knowledgeDTO,id);
   }


   @GetMapping("/getById/{pageNum}/{pageSize}")
   @ApiOperation("获取自个的心理知识")
   public Result getById(@PathVariable("pageNum") Integer pageNum,
                         @PathVariable("pageSize") Integer pageSize,
                         @RequestParam(required = false) String title
   ) {
       return psychologicalKnowledgeService.getMyKnowledge(pageNum,pageSize,title);
   }






}
